package com.sojpt.boot.controller.business;

import com.jfinal.aop.Before;
import com.jfinal.kit.HashKit;
import com.jfinal.kit.Kv;
import com.jfinal.kit.Ret;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.SqlPara;
import com.sojpt.boot.controller.business.validator.LkyStudentDuplicateValidator;
import com.sojpt.boot.controller.business.validator.LkyStudentUserAddValidator;
import com.sojpt.boot.ui.iview.IViewController;
import com.sojpt.kit.SoJLogKit;
import com.sojpt.model.DicPopedom;
import com.sojpt.model.DicUser;
import com.sojpt.model.DicUserRole;
import com.sojpt.util.ResultUtil;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

/**
 * Description: 本类用于学生注册
 * Created by JYN_LL on 2020/2/12 19:06
 */
@RestController
@RequestMapping("/admin/student/")
public class LkyStudentRegisterAdminController extends IViewController {

    /**
     * 学生注册账号
     * @return
     */
    @PostMapping("register")
    @Before({LkyStudentDuplicateValidator.class, LkyStudentUserAddValidator.class})
    public void registerStudent(String code){
        DicUser dicUser = getModel(DicUser.class, "student");
            //获取当前系统时间
            Timestamp d = new Timestamp(System.currentTimeMillis());
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String createTime=df.format(d.getTime());
            Timestamp timestamp=Timestamp.valueOf(createTime);
            //学生表需要添加的

            String password = dicUser.getPasswd();
            // 密码加盐 hash
            String salt = HashKit.generateSaltForSha256();
            password = HashKit.sha256(salt + password);
            dicUser.setSalt(salt);
            dicUser.setPasswd(password);
            dicUser.setActStatus(0);
            dicUser.setCreaterId("0");
            BigDecimal type=BigDecimal.valueOf(2);
            dicUser.setType(type);
            dicUser.setId(UUID.randomUUID().toString().replace("-", ""));
            dicUser.setEntryTime(new Date());
            dicUser.setCreationTime(timestamp);
            //进行数据库事务操作
            //1.把学生账号添加在user表中
            //2.把学生账号与未激活的学生角色链接
            Db.tx(() -> {
                dicUser.save();
                //获取角色表中的未激活学生id
                String roleName="未激活的学生";
                String roleid = Db.queryStr("select ID from dic_role where ROLE_NAME = ?",roleName);
                //创建角色与用户关系对象
                DicUserRole dicUserRole=new DicUserRole();
                dicUserRole.setId(UUID.randomUUID().toString().replace("-",""));
                dicUserRole.setUserId(dicUser.getId());
                dicUserRole.setRoleId(roleid);
                dicUserRole.setEntryTime(timestamp);
                dicUserRole.save();
                return true;
            });
            renderJson(Ret.ok());
        }



    /**
     * 检验学号唯一性
     */
    @PostMapping("OnlyCode")
    public void onlyCode(){
        DicUser dicUser = getModel(DicUser.class, "student");
        String mid = Db.queryStr("select count(*) from dic_user where USER_CODE = ?", dicUser.getUserCode());
        int count = Integer.parseInt(mid);
        if (count == 0){
            renderJson(0);
        } else {
          renderJson(1);
        }
    }
    /**
     * 忘记密码
     */
    @PostMapping("password")
    public void password(){
        DicUser dicUser = getModel(DicUser.class, "student");
        String USER_CODE=dicUser.getUserCode();
        String telphone= dicUser.getTelphone();
        String password = dicUser.getPasswd();
        // 密码加盐 hash
        String salt = HashKit.generateSaltForSha256();
        password = HashKit.sha256(salt + password);
        int count=Db.update("update dic_user set PASSWD=? where USER_CODE=? and telphone=?",password,USER_CODE,telphone);
        int count2=Db.update("update dic_user set salt=? where USER_CODE=? and telphone=?",salt,USER_CODE,telphone);
        if(count+count2==2){
            renderJson(Ret.ok());
        }else{
            renderJson(Ret.fail());
        }

    }

}
